<?php
/*********************/
/*                   */
/* http://www.ymd.cc */
/*********************/
/*    QQ: 11121426   */
/*                   */

/*********************/

class mdl_template extends modelFactory
{

    public function getList()
    {
        $handle = opendir(THEME_DIR);
        while (false !== ($item = readdir($handle))) {
            if (substr($item, 0, 1) != "." && is_dir(THEME_DIR . "/" . $item)) {
                $t[$item] = 1;
            }
        }
        closedir($handle);
        foreach ($this->db->select("select * from sdb_themes where theme in(\"" . implode("\",\"", array_keys($t)) . "\") order by stime desc") as $theme) {
            $themes[$theme['theme']] = $theme;
            unset($t[$theme['theme']]);
        }
        $objWg = $this->system->loadModel("content/widgets");
        foreach ($objWg->getLibs() as $w) {
            $widgets[$w['name']] = $w;
            $widgets[$w['name']]['installed'] = true;
        }
        foreach (array_keys($t) as $item) {
            if (file_exists(THEME_DIR . "/" . $item . "/theme.xml")) {
                $themes = array_merge(array(
                    $item => $this->initTheme($item)
                ), $themes);
            }
        }
        foreach ($this->db->select("SELECT base_file,widgets_type FROM sdb_widgets_set s") as $widset) {
            $tname = substr($widset['base_file'], $p = strpos($widset['base_file'], ":") + 1, strpos($widset['base_file'], "/") - $p);
            if ($themes[$tname]) {
                $themes[$tname]['widgets'][$widset['widgets_type']] = $widgets[$widset['widgets_type']] ? $widgets[$widset['widgets_type']] : array(
                    "label" => $widset['widgets_type'],
                    "name" => $widset['widgets_type'],
                    "installed" => false
                );
            }
        }
        return $themes;
    }

    public function tempalte_rename($old_name, $new_name, $theme)
    {
        return $this->db->query("update sdb_tpl_source set tpl_name=\"" . trim($new_name) . "\" where tpl_name=\"" . trim($old_name) . "\" and tpl_theme=\"" . trim($theme) . "\"");
    }

    public function get_template_path($theme, $tpl)
    {
        if (file_exists(THEME_DIR . "/" . $theme . "/" . $tpl)) {
            return "user:" . $theme . "/" . $tpl;
        } else {
            if (($default_template = $this->system->getConf("system.custom_template_default")) && file_exists(THEME_DIR . "/" . $theme . "/" . $default_template)) {
                return "user:" . $theme . "/" . $default_template;
            }
            return "user:" . $theme . "/default.html";
        }
    }

    public function remove_tpl($theme, $tpl)
    {
        $sql = "delete from sdb_tpl_source where tpl_theme=\"" . $theme . "\" and tpl_file=\"" . $tpl . "\"";
        return $this->db->exec($sql);
    }

    public function insert_tpl($aData)
    {
        if (is_array($aData) && !$this->db->selectrow("select * from sdb_tpl_source where tpl_file=\"" . $aData['tpl_file'] . "\" and tpl_theme=\"" . $aData['tpl_theme'] . "\"")) {
            $rs = $this->db->query("select * from sdb_tpl_source where 1");
            $sql = $this->db->GetInsertSQL($rs, $aData);
            return $this->db->exec($sql);
        }
    }

    public function makeXml($theme, $name)
    {
        if (file_put_contents(THEME_DIR . "/" . $theme . "/" . $name . ".xml", $this->__makeConfigFile($theme))) {
            return true;
        } else {
            return false;
        }
    }

    public function copy_tpl($copy_from, $copy_to, $file_name, $type)
    {
        if ($result = $this->db->select("select * from sdb_widgets_set where base_file=\"" . $copy_from['type'] . ":" . $copy_from['theme'] . "/" . $copy_from['tpl'] . "\"")) {
            foreach ($result as $key => $value) {
                $aData = $result[$key];
                unset($aData['widgets_id']);
                $aData['base_file'] = $copy_to['type'] . ":" . $copy_to['theme'] . "/" . $copy_to['tpl'];
                $rs = $this->db->query("select * from sdb_widgets_set where 1");
                $sql = $this->db->GetInsertSQL($rs, $aData);
                $this->db->query($sql);
            }
        }
        $template = array(
            "tpl_name" => $file_name,
            "tpl_file" => $file_name,
            "tpl_theme" => $copy_from['theme'],
            "tpl_type" => $type
        );
        $this->insert_tpl($template);
        return true;
    }

    public function remove($theme)
    {
        return $this->__removeDir(THEME_DIR . "/" . $theme);
    }

    public function remove_directory($dir)
    {
        if (substr($dir, -1, 1) == "/") {
            $dir = substr($dir, 0, strlen($dir) - 1);
        }
        if ($handle = opendir("{$dir}")) {
            while (false !== ($item = readdir($handle))) {
                if ($item != "." && $item != "..") {
                    if (is_dir("{$dir}/{$item}")) {
                        $this->remove_directory("{$dir}/{$item}");
                    } else {
                        unlink("{$dir}/{$item}");
                    }
                }
            }
            closedir($handle);
            rmdir($dir);
        }
    }

    public function previewImg($theme, $img = "preview.jpg")
    {
        $this->system->sfile(THEME_DIR . "/" . $theme . "/" . $img, "images/theme_dft.jpg", true);
    }

    public function allowUpload(&$message)
    {
        if (!function_exists("gzinflate")) {
            $message = "gzip";
            return $message;
        }
        if (!is_writable(THEME_DIR)) {
            $message = "writeable";
            return $message;
        }
        return true;
    }

    public function getThemes($theme)
    {
        $list = array("theme.xml", "theme-bak.xml");
        $views = array();
        foreach ($list as $v) {
            if (is_file(THEME_DIR . "/" . $theme . "/" . $v)) {
                $views[] = $v;
            }
        }
        return $views;
    }

    public function update_template($type, $id, $file, $template_type)
    {
        if (isset($file)) {
            $aData = array(
                "source_type" => $type,
                "source_id" => $id,
                "template_name" => $file,
                "template_type" => $template_type
            );
            $rs = $this->db->query("select * from sdb_template_relation where source_type=\"" . $type . "\" and source_id=\"" . $id . "\" and template_type=\"" . $template_type . "\"");
            $sSQL = $this->db->GetUpdateSQL($rs, $aData, true);
            if (!$sSQL || $this->db->query($sSQL)) {
                return true;
            } else {
                return false;
            }
        }
    }

    public function get_customer_template($template_type, $id, $source_type = null)
    {
        $where = array();
        if ($template_type) {
            $where[] = " template_type =\"" . $template_type . "\"";
        }
        if ($source_type) {
            $where[] = " source_type =\"" . $source_type . "\"";
        }
        if ($id) {
            $result = $this->db->selectrow("select template_name from sdb_template_relation where source_id=\"" . $id . "\" and " . implode(" and ", $where));
            if ($template_type == "product" && !$result) {
                $cat = $this->db->selectrow("select cat_id from sdb_goods where goods_id=" . $this->db->quote($id));
                if ($cat['parent_id']) {
                    $cat_id = substr($cat['cat_path'], 0, strpos($cat['cat_path'], ","));
                } else {
                    $cat_id = $cat['cat_id'];
                    $result = $this->db->selectrow("select template_name from sdb_template_relation where source_type =\"cat\" and source_id=\"" . $cat_id . "\" and template_type =\"" . $template_type . "\"");
                }
            }
        }
        $id = "PAGE:" . $id;
        $sSql = "select B.template_name from sdb_sitemaps as A LEFT JOIN sdb_template_relation as B  ON A.node_id = B.source_id where A.action =\"" . $id . " \"";
        if ($template_type) {
            $sSql .= " AND B.template_type =\"" . $template_type . "\"";
        }
        if ($source_type) {
            $sSql .= " AND B.source_type =\"" . $source_type . "\"";
        }
        $rs = $this->db->selectrow($sSql);
        if ($template_type == "page" && $id && isset($rs)) {
            $result = $rs;
        }
        return $result['template_name'];
    }

    public function set_template($type, $id, $file, $template_type)
    {
        if ($file) {
            $aData = array(
                "template_name" => $file,
                "source_type" => $type,
                "source_id" => $id,
                "template_type" => $template_type
            );
            $rs = $this->db->query("select * from sdb_template_relation where 1");
            $sSQL = $this->db->GetInsertSQL($rs, $aData);
            if (!$sSQL || $this->db->query($sSQL)) {
                return true;
            } else {
                return false;
            }
        }
    }

    public function getViews($theme)
    {
        if ($handle = opendir(THEME_DIR . "/" . $theme)) {
            $views = array();
            while (false !== ($file = readdir($handle))) {
                if ($file[0] !== "." && $file[0] !== "_" && is_file(THEME_DIR . "/" . $theme . "/" . $file) && (($t = strtolower(strstr($file, "."))) == ".html" || $t == ".htm")) {
                    $views[] = $file;
                }
            }
            closedir($handle);
            return $views;
        } else {
            return false;
        }
    }

    /*public function upload( $file, &$msg )
    {
                    if ( !$this->allowUpload( $msg ) )
                    {
                                    return false;
                    }
                    $tar = $this->system->loadModel( "utility/tar" );
                    $handle = fopen( $file['tmp_name'], "r" );
                    $contents = file_get_contents( $handle );
                    preg_match( "/\\<id\\>(.*?)\\<\\/id\\>/", $contents, $tar_name );
                    $filename = $tar_name[1] ? $tar_name[1] : time( );
                    if ( is_dir( THEME_DIR."/".trim( $filename ) ) )
                    {
                                    $filename = time( );
                    }
                    $sDir = $this->__buildDir( str_replace( "\\", "/", THEME_DIR."/".trim( $filename ) ) );
                    if ( $tar->openTAR( $file['tmp_name'] ) )
                    {
                                    if ( $tar->containsFile( "theme.xml" ) )
                                    {
                                                    do
                                                    {
                                                                    foreach ( $tar->files as $id => $file )
                                                                    {
                                                                                    do
                                                                                    {
                                                                                                    $tar_tmp_file = substr( $file['name'], strrpos( $file['name'], "." ) + 1 );
                                                                                                    if ( check_file_name( $tar_tmp_file ) )
                                                                                                    {
                                                                                                                    $fpath = $sDir.$file['name'];
                                                                                                                    if ( is_dir( dirname( $fpath ) ) )
                                                                                                                    {
                                                                                                                                    break;
                                                                                                                    }
                                                                                                                    else
                                                                                                                    {
                                                                                                                                    if ( mkdir_p( dirname( $fpath ) ) )
                                                                                                                                    {
                                                                                                                                                    file_put_contents( $fpath, $tar->getContents( $file ) );
                                                                                                                                    }
                                                                                                                                    else
                                                                                                                                    {
                                                                                                                                                    $msg = __( "权限不允许" );
                                                                                                                                    }
                                                                                                                    }
                                                                                                    }
                                                                                                    break;
                                                                                    }
                                                                                    return false;
                                                                    } while ( 0 );
                                                                    file_put_contents( $fpath, $tar->getContents( $file ) );
                                                    } while ( 1 );
                                                    $tar->closeTAR( );
                                                    if ( !( $config = $this->initTheme( basename( $sDir ), "", "upload" ) ) )
                                                    {
                                                                    $this->__removeDir( $sDir );
                                                                    $msg = __( "shopEx模板包创建失败" );
                                                                    return false;
                                                    }
                                                    return $config;
                                    }
                                    else
                                    {
                                                    $msg = __( "不是标准的shopEx模板包" );
                                                    return false;
                                    }
                    }
                    else
                    {
                                    $msg = __( "模板包已损坏，不是标准的shopEx模板包" ).$file['tmp_name'];
                                    return false;
                    }
    }*/
    //上传模板包文件并解压，然后进行模板文件映射，即模板安装
    public function upload($file, &$msg)
    {
        if (!$this->allowUpload($msg)) {
            return false;
        }
        $tar = $this->system->loadModel("utility/tar");
        $handle = fopen($file['tmp_name'], "r");
        $contents = file_get_contents($handle);
        preg_match("/\\<id\\>(.*?)\\<\\/id\\>/", $contents, $tar_name);
        $filename = $tar_name[1] ? $tar_name[1] : time();
        if (is_dir(THEME_DIR . "/" . trim($filename))) {
            $filename = time();
        }
        $sDir = $this->__buildDir(str_replace("\\", "/", THEME_DIR . "/" . trim($filename)));
        if ($tar->openTAR($file['tmp_name'])) {
            if ($tar->containsFile("theme.xml")) {
                foreach ($tar->files as $id => $file) {
                    $tar_tmp_file = substr($file['name'], strrpos($file['name'], ".") + 1);
                    if (check_file_name($tar_tmp_file)) {
                        $fpath = $sDir . $file['name'];
                        if (!is_dir(dirname($fpath))) {
                            if (mkdir_p(dirname($fpath))) {
                                file_put_contents($fpath, $tar->getContents($file));
                            } else {
                                $msg = __("权限不允许");
                            }
                        }
                        file_put_contents($fpath, $tar->getContents($file));
                    }

                }
                $tar->closeTAR();
                if (!($config = $this->initTheme(basename($sDir), "", "upload"))) {
                    $this->__removeDir($sDir);
                    $msg = __("shopEx模板包创建失败");
                    return false;
                }
                return $config;
            } else {
                $msg = __("不是标准的shopEx模板包");
                return false;
            }
        } else {
            $msg = __("模板包已损坏，不是标准的shopEx模板包") . $file['tmp_name'];
            return false;
        }
    }

    public function getBorderFromThemes($file)
    {
        $wights_border = array();
        $path = THEME_DIR . "/" . $file;
        $workdir = getcwd();
        chdir($path);
        $xml = $this->system->loadModel("utility/xml");
        $content = file_get_contents("info.xml");
        $config = $xml->xml2arrayValues($content);
        foreach ($config['theme']['borders']['set'] as $k => $v) {
            $wights_border[$v['attr']['tpl']] = file_get_contents($v['attr']['tpl']);
        }
        chdir($workdir);
        return $wights_border;
    }

    public function resetTheme($theme)
    {
        $path = THEME_DIR . "/" . $theme;
        $workdir = getcwd();
        chdir($path);
        if (is_file("info.xml")) {
            $xml = $this->system->loadModel("utility/xml");
            $config = $xml->xml2arrayValues(file_get_contents("info.xml"));
            $aTheme = array(
                "theme" => $theme,
                "name" => $config['theme']['name']['value'],
                "id" => $config['theme']['id']['value'],
                "version" => $config['theme']['version']['value'],
                "info" => $config['theme']['info']['value'],
                "author" => $config['theme']['author']['value'],
                "site" => $config['theme']['site']['value'],
                "update_url" => $config['theme']['update_url']['value'],
                "config" => array(
                    "config" => $this->__changeXMLArray($config['theme']['config']['set']),
                    "borders" => $this->__changeXMLArray($config['theme']['borders']['set']),
                    "views" => $this->__changeXMLArray($config['theme']['views']['view'])
                )
            );
            chdir($workdir);
            if (!$this->updateThemes($aTheme)) {
                return false;
            } else {
                return true;
            }
        }
    }

    public function initTheme($theme, $replaceWg = false, $upload = "", $loadxml = "")
    {
        if (empty($loadxml)) {
            $loadxml = "theme.xml";
        }
        $configxml = "info.xml";
        $this->separatXml($theme);
        $sDir = THEME_DIR . "/" . $theme . "/";
        $xml = $this->system->loadModel("utility/xml");
        $wightes_info = $xml->xml2arrayValues(file_get_contents($sDir . $loadxml));
        if (is_file($sDir . $configxml)) {
            $config = $xml->xml2arrayValues(file_get_contents($sDir . $configxml));
        } else {
            $config = $wightes_info;
        }
        if ($upload == "upload" && $config['theme']['id']['value']) {
            $config['theme']['id']['value'] = preg_replace("@[^a-zA-Z0-9]@", "_", $config['theme']['id']['value']);
            if (file_rename(THEME_DIR . "/" . $theme, THEME_DIR . "/" . $config['theme']['id']['value'])) {
                $sDir = THEME_DIR . "/" . $config['theme']['id']['value'];
                $theme = $config['theme']['id']['value'];
                $replaceWg = false;
            }
        }
        $aTheme = array(
            "name" => $config['theme']['name']['value'],
            "id" => $config['theme']['id']['value'],
            "version" => $config['theme']['version']['value'],
            "info" => $config['theme']['info']['value'],
            "author" => $config['theme']['author']['value'],
            "site" => $config['theme']['site']['value'],
            "update_url" => $config['theme']['update_url']['value'],
            "config" => array(
                "config" => $this->__changeXMLArray($config['theme']['config']['set']),
                "borders" => $this->__changeXMLArray($config['theme']['borders']['set']),
                "views" => $this->__changeXMLArray($config['theme']['views']['view'])
            )
        );
        $aWidgets = $wightes_info['theme']['widgets']['widget'];
        $aTheme['theme'] = $theme;
        $aTheme['stime'] = time();
        if (is_array($aTheme['config']['views']) && 0 < count($aTheme['config']['views'])) {
            foreach ($aTheme['config']['views'] as $v) {
                $tmp[] = $v['tpl'];
            }
            $aTheme['template'] = implode(",", $tmp);
        } else {
            $aTheme['template'] = "";
        }
        $i = 0;
        for (; $i < count($aWidgets); ++$i) {
            $aTmp[$i]['base_file'] = "user:" . $aTheme['theme'] . "/" . $aWidgets[$i]['attr']['file'];
            $aTmp[$i]['base_slot'] = $aWidgets[$i]['attr']['slot'];
            $aTmp[$i]['widgets_type'] = $aWidgets[$i]['attr']['type'];
            $aTmp[$i]['widgets_order'] = $aWidgets[$i]['attr']['order'];
            $aTmp[$i]['title'] = $aWidgets[$i]['attr']['title'];
            $aTmp[$i]['domid'] = $aWidgets[$i]['attr']['domid'];
            $aTmp[$i]['border'] = $aWidgets[$i]['attr']['border'];
            $aTmp[$i]['classname'] = $aWidgets[$i]['attr']['classname'];
            $aTmp[$i]['tpl'] = $aWidgets[$i]['attr']['tpl'];
            $aTmp[$i]['base_id'] = $aWidgets[$i]['attr']['baseid'];
            $aTmp[$i]['params'] = strtr($aWidgets[$i]['value'], array_flip(get_html_translation_table(HTML_SPECIALCHARS)));
        }
        $aWidgets = $aTmp;
        $aNumber = $this->countWidgetsByTheme($theme);
        $nNumber = intval($aNumber['num']);
        $insertWidgets = false;
        if ($replaceWg) {
            if ($nNumber) {
                $this->deleteWidgetsByTheme($theme);
            }
            $insertWidgets = true;
        } else if ($nNumber == 0) {
            $insertWidgets = true;
        }
        if ($insertWidgets && 0 < count($aWidgets)) {
            foreach ($aWidgets as $k => $wg) {
                $this->insertWidgets($wg);
            }
        }
        if (!$this->updateThemes($aTheme)) {
            return false;
        } else {
            return $aTheme;
        }
    }

    public function countWidgetsByTheme($sTheme)
    {
        return $this->db->selectrow("select count(\"widgets_id\") as num from sdb_widgets_set where  base_file like \"user:" . $sTheme . "%\"");
    }

    public function deleteWidgetsByTheme($sTheme)
    {
        return $this->db->query("delete from sdb_widgets_set where  base_file like \"user:" . $sTheme . "%\"");
    }

    public function insertWidgets($aData)
    {
        $rs = $this->db->query("select * from sdb_widgets_set where 1");
        $aData['params'] = str_replace("'", "\\'", $aData['params']);
        $sSQL = $this->db->GetInsertSQL($rs, $aData);
        if (!$sSQL || $this->db->query($sSQL)) {
            return true;
        } else {
            return false;
        }
    }

    public function __buildDir($sDir)
    {
        if (file_exists($sDir)) {
            $aTmp = explode("/", $sDir);
            $sTmp = end($aTmp);
            if (strpos($sTmp, "(")) {
                $i = substr($sTmp, strpos($sTmp, "(") + 1, -1);
                ++$i;
                $sDir = str_replace(("(" . ($i - 1)) . ")", "(" . $i . ")", $sDir);
            } else {
                $sDir .= "(1)";
            }
            return $this->__buildDir($sDir);
        } else {
            return $sDir . "/";
        }
    }

    public function __removeDir($sDir)
    {
        if ($rHandle = opendir($sDir)) {
            while (false !== ($sItem = readdir($rHandle))) {
                if ($sItem != "." && $sItem != "..") {
                    if (is_dir($sDir . "/" . $sItem)) {
                        $this->__removeDir($sDir . "/" . $sItem);
                    } else if (!unlink($sDir . "/" . $sItem)) {
                        trigger_error(__("因权限原因，模板文件") . $sDir . "/" . $sItem . __("无法删除"), E_USER_NOTICE);
                    }
                }
            }
            closedir($rHandle);
            rmdir($sDir);
            return true;
        } else {
            return false;
        }
    }

    public function updateThemes($aData)
    {
        $rs = $this->db->query("select * from sdb_themes where theme=\"" . $aData['theme'] . "\"");
        $sql = $this->db->GetUpdateSQL($rs, $aData, true);
        return !$sql || $this->db->exec($sql);
    }

    public function setDefault($theme)
    {
        return $this->system->setConf("system.ui.current_theme", $theme);
    }

    public function reset($theme, $xml = "")
    {
        return $this->initTheme($theme, true, "", $xml);
    }

    public function getDefault()
    {
        $defaultTheme = $this->system->getConf("system.ui.current_theme");
        $this->separatXml($defaultTheme);
        return $defaultTheme;
    }

    public function separatXml($theme)
    {
        $workdir = getcwd();
        chdir(THEME_DIR . "/" . $theme);
        if (!is_file("info.xml")) {
            $content = file_get_contents("theme.xml");
            $rContent = substr($content, 0, strpos($content, "<widgets>"));
            file_put_contents("info.xml", $rContent . "</theme>");
        }
        chdir($workdir);
    }

    public function outputPkg($theme)
    {
        $tar = $this->system->loadModel("utility/tar");
        $workdir = getcwd();
        if (chdir(THEME_DIR . "/" . $theme)) {
            $this->__getAllFiles(".", $aFile);
            $i = 0;
            for (; $i < count($aFile); ++$i) {
                if (!($f = substr($aFile[$i], 2)) && !($f != "theme.xml")) {
                    $tar->addFile($f);
                }
            }
            if (is_file("info.xml")) {
                $tar->addFile("info.xml", file_get_contents("info.xml"));
            }
            $tar->addFile("theme.xml", $this->__makeConfigFile($theme));
            $aTheme = $this->getThemeInfo($theme);
            $this->system->__session_close();
            $charset = $this->system->loadModel("utility/charset");
            $name = $charset->utf2local(preg_replace("/\\s/", "-", $aTheme['name'] . "-" . $aTheme['version']), "zh");
            @set_time_limit(0);
            header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
            header("Content-type: application/octet-stream");
            header("Content-type: application/force-download");
            header("Content-Disposition: attachment; filename=\"" . $name . ".tgz\"");
            $tar->getTar("output");
            chdir($workdir);
        } else {
            chdir($workdir);
            return false;
        }
    }

    public function __getAllFiles($sDir, &$aFile, $loop = true)
    {
        if ($rHandle = opendir($sDir)) {
            while (false !== ($sItem = readdir($rHandle))) {
                if ($sItem != "." && $sItem != ".." && $sItem != "") {
                    if (is_dir($sDir . "/" . $sItem)) {
                        if ($loop) {
                            $this->__getAllFiles($sDir . "/" . $sItem, $aFile);
                        }
                    } else {
                        $aFile[] = $sDir . "/" . $sItem;
                    }
                }
            }
            closedir($rHandle);
        }
    }

    public function __makeConfigFile($sTheme)
    {
        $aTheme = $this->getThemeInfo($sTheme);
        $aWidget['widgets'] = $this->getWidgetsInfo($sTheme);
        foreach ($aWidget['widgets'] as $i => $widget) {
            $aWidget['widgets'][$i]['params'] = $aWidget['widgets'][$i]['params'];
            $aWidget['widgets'][$i]['base_file'] = str_replace("user:" . $sTheme . "/", "", $aWidget['widgets'][$i]['base_file']);
        }
        $aTheme['config']['config'] = $aTheme['config']['config'];
        $aTheme['config']['views'] = $aTheme['views'];
        $aTheme['id'] = $aTheme['theme'];
        $smarty =& $this->system->loadModel("system/frontend");
        $smarty->template_dir = CORE_DIR . "/admin/view/";
        $aTheme = array_merge($aTheme, $aWidget);
        $sXML = $smarty->_fetch_compile_include("system/template/theme.xml", $aTheme);
        return $sXML;
    }

    public function __changeXMLArray($aArray)
    {
        $aData = array();
        foreach ($aArray as $i => $v) {
            unset($v['attr']);
            $aData[$i] = array_merge($v, $aArray[$i]['attr']);
        }
        return $aData;
    }

    public function getThemeInfo($sTheme)
    {
        $info = $this->db->selectrow("select * from sdb_themes where theme=\"" . $sTheme . "\"");
        if (isset($info['config'])) {
            $info['config'] = unserialize($info['config']);
        }
        return $info;
    }

    public function getWidgetsInfo($sName)
    {
        return $this->db->select("select * from sdb_widgets_set where base_file like \"user:" . $sName . "/%\"");
    }

    public function htmEdit($src)
    {
        return $this->db->selectrow("select * from sdb_pages where page_title=\"" . $src . "\"");
    }

    public function editHtml($title, $data)
    {
        $dat = $this->db->selectrow("select * from sdb_pages where page_title=\"" . $title . "\"");
        if ($dat['page_title']) {
            $rs = $this->db->query("select * from sdb_pages where page_title=\"" . $title . "\"");
            $sql = $this->db->GetUpdateSQL($rs, $data);
        } else {
            $rs = $this->db->query("select * from sdb_pages where 1");
            $sql = $this->db->GetInsertSQL($rs, array_merge($data, array(
                "page_title" => $title
            )));
        }
        return !$sql || $this->db->exec($sql);
    }

    public function applyTheme($theme = null)
    {
        if (!$theme) {
            $theme = $this->system->getConf("system.ui.current_theme");
            define("TPL_ID", $theme);
        }
        return $this->getThemeInfo($theme);
    }

    public function getContent($theme, $file)
    {
        return file_get_contents(THEME_DIR . "/" . $theme . "/" . $file);
    }

    public function delFile($theme, $file)
    {
        return unlink(THEME_DIR . "/" . $theme . "/" . $file);
    }

    public function del_template_widgets($theme, $file, $type = "user")
    {
        return $this->db->query("delete from sdb_widgets_set where base_file =\"" . $type . ":" . $theme . "/" . $file . "\"");
    }

    public function recoverTpl($theme, $bakfile, $file)
    {
        $dir = THEME_DIR . "/" . $theme . "/";
        return copy($dir . $bakfile, $dir . $file);
    }

    public function setContent($theme, $file, $content, $isbak = false)
    {
        if ($content) {
            $this->__getAllFiles(THEME_DIR . "/" . $theme, $aFile, false);
            if (count($aFile) == 0 || $isbak) {
                $aTmp = explode(".", $file);
                $arrNum = count($aTmp) - 1;
                $lastStr = $aTmp[$arrNum];
                if (substr($aTmp[$arrNum - 1], 0, 4) == "bak_") {
                    $arrNum -= 1;
                }
                $i = 0;
                for (; $i < $arrNum; ++$i) {
                    $preStr .= $aTmp[$i] . ".";
                }
                $iLoop = 1;
                foreach ($aFile as $item) {
                    if (strstr($item, $preStr . "bak_")) {
                        preg_match_all("/" . $preStr . "bak_(.*)\\./i", $item, $match);
                        if ($iLoop <= $match[1][0]) {
                            $iLoop = $match[1][0] + 1;
                        }
                    }
                }
                if ($isbak) {
                    $saveFile = $preStr . "bak_" . $iLoop . "." . $lastStr;
                    file_rename(THEME_DIR . "/" . $theme . "/" . $file, THEME_DIR . "/" . $theme . "/" . $saveFile);
                }
            }
            file_put_contents(THEME_DIR . "/" . $theme . "/" . $file, stripslashes($content));
            return 1;
        } else {
            return false;
        }
    }

    public function getListName($name)
    {
        $ctl = array(
            "default.html" => __("其它页面"),
            "index.html" => __("首页"),
            "article.html" => __("文章页"),
            "product.html" => __("商品详细页"),
            "comment.html" => __("商品评论/咨询页"),
            "gallery.html" => __("商品列表页"),
            "cart.html" => __("购物车页"),
            "gift.html" => __("赠品页"),
            "member.html" => __("会员中心页"),
            "page.html" => __("站点栏目单独页"),
            "passport.html" => __("注册/登录页"),
            "search.html" => __("高级搜索页")
        );
        return $ctl[$name];
    }

    public function get_template_list($theme)
    {
        $result = $this->db->select("select * from sdb_tpl_source where tpl_theme=\"" . $theme . "\"");
        $list = array();
        foreach ($result as $key => $value) {
            $list[$value['tpl_type']][] = array(
                "tpl_name" => $value['tpl_name'],
                "tpl_file" => $value['tpl_file']
            );
        }
        return $list;
    }

    public function geteditlist($theme)
    {
        $list = array();
        $this->__getAllFiles(THEME_DIR . "/" . $theme, $list, false);
        $ctl = $this->getname();
        if (file_exists(THEME_DIR . "/" . $theme . "/cart.html")) {
            if (!file_exists(THEME_DIR . "/" . $theme . "/order_detail.html")) {
                copy(THEME_DIR . "/" . $theme . "/cart.html", THEME_DIR . "/" . $theme . "/order_detail.html");
            }
            if (!file_exists(THEME_DIR . "/" . $theme . "/order_index.html")) {
                copy(THEME_DIR . "/" . $theme . "/cart.html", THEME_DIR . "/" . $theme . "/order_index.html");
            }
        }
        foreach (( array )$list as $key => $value) {
            $file_name = basename($value, ".html");
            if (!strpos($file_name, ".")) {
                if ($pos = strpos($file_name, "-")) {
                    $type = substr($file_name, 0, $pos);
                    $file[$type][$key]['name'] = $ctl[substr($file_name, 0, $pos)];
                    $file[$type][$key]['file'] = $file_name . ".html";
                } else {
                    $type = $file_name;
                    $file[$file_name][$key]['name'] = $ctl[$file_name];
                    $file[$file_name][$key]['file'] = $file_name . ".html";
                }
                if ($type && !$this->system->getConf("system.custom_template_" . $type) && $file_name != "default-pro") {
                    $this->system->setConf("system.custom_template_" . $type, $file_name . ".html");
                }
                $template = array(
                    "tpl_name" => $file_name . ".html",
                    "tpl_file" => $file_name . ".html",
                    "tpl_theme" => $theme,
                    "tpl_type" => $type ? $type : $file_name
                );
                $this->insert_tpl($template);
            }
        }
        return $file;
    }

    public function get_bak_file($theme, $file)
    {
        $list = array();
        $this->__getAllFiles(THEME_DIR . "/" . $theme, $list, false);
        $file = basename($file, ".html");
        foreach (( array )$list as $key => $value) {
            $file_name = basename($value, ".html");
            if (strstr($file_name, $file . ".") && preg_match("/.*\\.bak_[0-9]+\\.[^\\.]+/", $value)) {
                $template[] = array(
                    "tpl_name" => $file_name . ".html",
                    "tpl_file" => $value,
                    "tpl_theme" => $theme,
                    "tpl_type" => $type ? $type : $file_name
                );
            }
        }
        return $template;
    }

    public function getname()
    {
        $ctl = array(
            "index" => __("首页"),
            "gallery" => __("商品列表页"),
            "product" => __("商品详细页"),
            "comment" => __("商品评论/咨询页"),
            "article" => __("文章页"),
            "article" => __("文章列表页"),
            "gift" => __("赠品页"),
            "package" => __("捆绑商品页"),
            "brandlist" => __("品牌专区页"),
            "brand" => __("品牌商品展示页"),
            "cart" => __("购物车页"),
            "search" => __("高级搜索页"),
            "passport" => __("注册/登录页"),
            "member" => __("会员中心页"),
            "page" => __("站点栏目单独页"),
            "order_detail" => __("订单详细页"),
            "order_index" => __("订单确认页"),
            "default" => __("默认页")
        );
        return $ctl;
    }

    public function templateList($theme)
    {
        include("shopctls.php");
        $ctl = $this->getname();
        foreach ($ctl as $c => $a) {
            if ($ctl[$c]) {
                $item = array(
                    "name" => $ctl[$c],
                    "items" => array(),
                    "file" => $file = $c . ".html",
                    "available" => file_exists(THEME_DIR . "/" . $theme . "/" . $file)
                );
                foreach ($a as $act => $i) {
                    $item['items'][] = array_merge(array(
                        "act" => $act,
                        "file" => $file = $c . "-" . $act . ".html",
                        "available" => file_exists(THEME_DIR . "/" . $theme . "/" . $file)
                    ), $i);
                }
                $data[] = $item;
            }
        }
        return $data;
    }

    public function getWdigetsPage()
    {
        return $this->db->select("SELECT * FROM sdb_widgets_set WHERE base_file NOT LIKE \"user:%\"");
    }

    public function updateWidgets($aData)
    {
        $rs = $this->db->query("SELECT * FROM sdb_widgets_set WHERE widgets_id = " . $aData['widgets_id']);
        $sql = $this->db->GetUpdateSQL($rs, $aData, true);
        return !$sql || $this->db->exec($sql);
    }

    public function getTemplateByType($tpl_type)
    {
        $rs = $this->db->selectrow("SELECT tpl_type  FROM sdb_tpl_source  WHERE tpl_type  = \"" . $tpl_type . "\"");
        if (isset($rs)) {
            return true;
        } else {
            return false;
        }
    }

}

?>
